package flappy.bird.cover.ui;

import java.io.IOException;
import java.util.Random;

import javax.microedition.lcdui.Command;
import javax.microedition.lcdui.CommandListener;
import javax.microedition.lcdui.Displayable;
import javax.microedition.lcdui.Graphics;
import javax.microedition.lcdui.Image;
import javax.microedition.lcdui.game.GameCanvas;
import javax.microedition.lcdui.game.LayerManager;
import javax.microedition.lcdui.game.Sprite;
import javax.microedition.midlet.MIDletStateChangeException;

import flappy.bird.cover.component.Pipe;
import flappy.bird.cover.resource.AppResource;

public class GameUI extends GameCanvas implements Runnable {
	private Main midlet;
	private Graphics g;
	private Pipe pipe1, pipe2, pipe3;
	private boolean isRunning = false;
	private int iPostionPipe1, iPostionPipe2, iPostionPipe3, iPostionPipe4, i;
	final int DISTANCE_X = 130, DISTANCE_Y = 85;
	final int WIDTH_SPRITE = 50, HEIGHT_SPRITE = 252;
	private int rnd1 = 80, rnd2 = 110, rnd3 = 130;
	private boolean move, change,check1=true,check2=true,check3=true;
	private int interval;
	private int k, x, y, ii,diem=0;
	private int valueUp = 50, valueDown = 2, giatocUp = 7, giatocDown = 0;

	protected GameUI() {
		super(true);
		this.midlet = midlet;
		setFullScreenMode(true);
		interval = 0;
		k = 0;
		x = 70;
		y = 0;
	}

	public void unload() {

	}

	public void load() {
		pipe1 = new Pipe();
		pipe2 = new Pipe();
		pipe3 = new Pipe();
	}

	public void start() {
		isRunning = true;
		move = true;
		Thread t = new Thread(this);
		t.start();
	}

	public void paint(Graphics g) {
	}

	public void run() {
		Graphics g = getGraphics();

		this.i = 0;
		this.iPostionPipe1 = getWidth();
		this.iPostionPipe2 = getWidth() + DISTANCE_X;
		this.iPostionPipe3 = getWidth() + DISTANCE_X * 2;
		while (isRunning && move) {
			// Update display
			

			this.i -= 2;
			this.iPostionPipe1 -= 2;
			this.iPostionPipe2 -= 2;
			this.iPostionPipe3 -= 2;
			if (this.i < -320)
				this.i = 0;
			if (this.iPostionPipe1 < -WIDTH_SPRITE - DISTANCE_X) {
				this.iPostionPipe1 = getWidth();
				rnd1 = getRandom();
			}
			if (this.iPostionPipe2 < -WIDTH_SPRITE - DISTANCE_X) {
				this.iPostionPipe2 = getWidth();
				rnd2 = getRandom();
			}
			if (this.iPostionPipe3 < -WIDTH_SPRITE - DISTANCE_X) {
				this.iPostionPipe3 = getWidth();
				rnd3 = getRandom();
			}
			if (change) {// bay len
				if (valueUp >= 0) {
					valueUp--;
					this.y -= 3 + giatocUp;
					giatocDown = 0;
					giatocUp -= 1;
					if (giatocUp <= 0)
						giatocUp = 0;
					if (this.y < 0)
						this.y = 0;
				} else
					change = false;
			} else {// roi xuong
				giatocDown++;
				giatocUp = 7;
				if (giatocDown >= 10)
					giatocDown = 10;
				this.y += 1 + giatocDown;
				if (this.y >= 270)
				{
					this.y = 270;
					move = false;
				}
			}

			ii++;
			if (ii >= 2) {
				this.k++;
				if (this.k >= 3)
					this.k = 0;

				ii = 0;
			}
			//xu ly va cham
			if ((this.iPostionPipe1<this.x+28)&&(this.x<this.iPostionPipe1+WIDTH_SPRITE))
			{
				if ((this.y<rnd1)||(this.y+20>rnd1+DISTANCE_Y))
					move = false;
				
				if (this.x>this.iPostionPipe1+WIDTH_SPRITE/2) 
				{
					if (check1==true) diem++;
					System.out.println("aaaaaaaaaa");
					check1=false;
					check2=true;
					check3=true;
				}
				
			}
			else
				if ((this.iPostionPipe2<this.x+28)&&(this.x<this.iPostionPipe2+WIDTH_SPRITE))
				{
					if ((this.y<rnd2)||(this.y+20>rnd2+DISTANCE_Y))
						move = false;
					if (this.x>this.iPostionPipe2+WIDTH_SPRITE/2) 
					{
						if (check2==true) diem++;
						check1=true;
						check2=false;
						check3=true;
					}
				}
				else
					if ((this.iPostionPipe3<this.x+28)&&(this.x<this.iPostionPipe3+WIDTH_SPRITE))
					{
						if ((this.y<rnd3)||(this.y+20>rnd3+DISTANCE_Y))
							move = false;
						if (this.x>this.iPostionPipe3+WIDTH_SPRITE/2-14) 
						{
							if (check3==true) diem++;
							check1=true;
							check2=true;
							check3=false;
						}
					}
			System.out.println(diem);
			drawDisplay(g, this.iPostionPipe1, iPostionPipe2, iPostionPipe3,
					this.i, rnd1, rnd2, rnd3);
			render(g, this.k);
			flushGraphics();
			try {
				Thread.sleep(0);
			} catch (InterruptedException ie) {
			}
		}
		while (this.y<262)
		{
			this.y+=10;
			if (this.y>262) this.y=262;
			drawDisplay(g, this.iPostionPipe1, iPostionPipe2, iPostionPipe3,
					this.i, rnd1, rnd2, rnd3);
			render(g, 3);
			flushGraphics();
			try {
				Thread.sleep(0);
			} catch (InterruptedException ie) {
			}
		}
		setDie();
		
	}
	
	public void setDie()
	{
		System.out.println("dieeeeeeeeee");
	}
	

	public int getRandom() {
		int rndNumber;
		Random rnd = new Random();
		do {
			rndNumber = rnd.nextInt(200);
		} while (rndNumber <= 60 || rndNumber >= 180);
		return rndNumber;
	}

	private void drawDisplay(Graphics g, int iPostionPipe1, int iPostionPipe2,
			int iPostionPipe3, int i, int rnd1, int rnd2, int rnd3) {
		load();

		g.drawImage(AppResource.background, 0, getHeight(), Graphics.LEFT
				| Graphics.BOTTOM);
		// 252-50
		g.drawImage(pipe1.getImgPiPeTop(), iPostionPipe1, rnd1,
				Graphics.LEFT | Graphics.BOTTOM);
		g.drawImage(pipe1.getImgPiPeBottom(), iPostionPipe1, rnd1 + DISTANCE_Y,
				Graphics.LEFT | Graphics.TOP);

		g.drawImage(pipe2.getImgPiPeTop(), iPostionPipe2, rnd2,
				Graphics.LEFT | Graphics.BOTTOM);
		g.drawImage(pipe2.getImgPiPeBottom(), iPostionPipe2, rnd2 + DISTANCE_Y,
				Graphics.LEFT | Graphics.TOP);
		//
		g.drawImage(pipe3.getImgPiPeTop(), iPostionPipe3, rnd3,
				Graphics.LEFT | Graphics.BOTTOM);
		g.drawImage(pipe3.getImgPiPeBottom(), iPostionPipe3, rnd3 + DISTANCE_Y,
				Graphics.LEFT | Graphics.TOP);

		// chay background bottom
		g.drawImage(AppResource.background_bottom, i, 290, Graphics.LEFT
				| Graphics.TOP);
		flushGraphics();
	}

	public void render(Graphics g, int k) {
		Image tmp;
		tmp = Image.createImage(AppResource.splitted[k], 0, 0,
				AppResource.splitted[k].getWidth(),
				AppResource.splitted[k].getHeight(), 0);
		g.drawImage(tmp, this.x, this.y, Graphics.TOP | Graphics.LEFT);
	}

	public void pointerPressed(int x, int y) {
		change = true;
		valueUp = 7;
		giatocUp = 7;
	}

	/*--------------------------------------------------
	 * Stop thread
	 *-------------------------------------------------*/
	public void stop() {
		isRunning = false;
		move = false;
		change = false;
	}
}
